/*
 * Block comments with details of changes
 */
public without sharing  class WorkOrderProjectController
{
    //获取仪器所有的工单项目
    public List<WorkOrderProjectBean> apList{get;set;}
    //工单项目对象 视图使用放大镜的时候会使用到
    public WorkOrderItems__c ap{get;set;}
    //工单的Id
    public String wid{get;set;}
    //工单
    public WorkOrder__c w ;
    //获取仪器的工单项目列表  显示出来
    public List<WorkOrderItems__c> oldList{get;set;}
    
    public WorkOrderProjectController(ApexPages.StandardController stdController)
    {
        //获得仪器id
        wid = System.currentPageReference().getParameters().get('wid');
        apList=new List<WorkOrderProjectBean>();
        ap=new WorkOrderItems__c();
        w =new WorkOrder__c();
    }
    
    public void init(){
        this.getOldList();
        this.getAPList();
    }
   
   
    //获得已经保存的数据
    public void getOldList(){
        if (String.isBlank(wid) == false){
            Schema.DescribeSobjectResult m = WorkOrderItems__c.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> fieldMap = m.fields.getMap();
            
            String sql = 'select ';
            String fields = '';
            for (String field : fieldMap.keySet()) {
                if (fields.length() > 0) {
                    fields += ', '; 
                }
                fields += field;
            }
            sql += fields;
            sql += ' from WorkOrderItems__c where WorkOrder__c= \'' +wid+ '\'';
            oldList=Database.query(sql);
            System.debug('=========='+oldList);
        }
    }
     
    //获得该仪器所有的产品
    public void getAPList(){
             w = [select Id,ServiceInstrumentCP__c from WorkOrder__c where Id =: wid];
        //System.debug('===========================getAPList' +w+'\t '+ w.ServiceManufacture__c  );
        System.debug(System.currentPageReference().getParameters().get('eid'));
      List<Product__c> eqList=[select Name,Category1__c,Category2__c from Product__c where Id=:w.ServiceInstrumentCP__c];
     // List<Equipment__c> eqList=[select Name,Product__r.Category1__c,Product__r.Category2__c,Supplier__c,Account__c from Equipment__c where Id=:w.ServiceManufacture__c];
        if(eqList.size()>0){
           //Equipment__c eq=eqList[0];
            Product__c product=eqList[0];//获得产品
            
            String [] arr = product.Category2__c.split('-',2);
            String category2='%'+arr[0]+'-%';
            
            //根据产品获得试剂项目
            //2017-03-07修改如下
            System.debug('==========='+category2);
            List<ProductProject__c> ppList=[select Name,Id from ProductProject__c where (ThirdClassification__c='试剂' or ThirdClassification__c='试纸') 
                                            and PrimaryClassification__c=:product.Category1__c
                                            and SecondClassification__c like :category2];
            
      
            for(ProductProject__c pp:ppList){
                Boolean flag = false;
               WorkOrderProjectBean apb=new WorkOrderProjectBean();
               apb.rec.Items__c=pp.Id;
                apb.rec.WorkOrder__c = wid;
                apb.projectName = pp.Name;
               //把该仪器存在的数据 返回 赋值
               for(WorkOrderItems__c old:oldList){
                   if(pp.Id==old.Items__c){
                       flag = true;
                       apb.rec.WorkOrderBrand__c=old.WorkOrderBrand__c;
                       apb.projectName = pp.Name;
                       apb.rec.ProjectReasons__c = old.ProjectReasons__c; 
                       apb.chk=true;
                       apb.selected=true;
                       break;
                   }
                   
               }
               if(flag == true && apList.size() > 0){
                     apList.add(0,apb);
                }else{
                    apList.add(apb);
                }
       
            }
          
        }
    }
    

    //保存
    public void save1() {
        Savepoint sp = Database.setSavepoint();
        try {
            //删除该仪器下所有的资产项目
            List<WorkOrderItems__c> delList = [select Id from WorkOrderItems__c where WorkOrder__c= :wid];
            if (delList.size() > 0) delete delList;
            
            // 插入已选择的物品
            if (apList.size() > 0) {
                List<WorkOrderItems__c> insList = new List<WorkOrderItems__c>();
                for (WorkOrderProjectBean apb : apList) {
                    if(apb.selected){
                     insList.add(
                        new WorkOrderItems__c(
                            WorkOrderBrand__c =apb.rec.WorkOrderBrand__c, //品牌
                 			WorkOrder__c = apb.rec.WorkOrder__c,
                            Items__c = apb.rec.Items__c,
                            ProjectReasons__c = apb.rec.ProjectReasons__c
                        )
                      );
                    }
                }
                
                insert insList;
            }
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, System.Label.Save_OK));
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, e.getMessage()));
        }
    }
    
    public class WorkOrderProjectBean{
        public WorkOrderItems__c rec{get;set;}
        public String projectName {get;set;}//项目名称
        public Boolean chk{get;set;}
        public Boolean selected{get;set;}//选中保存项

        public WorkOrderProjectBean() {
            rec=new WorkOrderItems__c();
        }
    }
}